package com.it.flowable.listener.leave;

import com.it.flowable.common.constants.BpmCommonConstant;
import com.it.flowable.entity.leave.ActFlwServiceDataLeaveVO;
import com.it.flowable.service.common.BpmSpringUtils;
import com.it.flowable.service.leave.bpm.BpmLeaveDao;
import com.it.flowable.service.leave.bpm.BpmLeaveTaskService;
import com.it.flowable.service.leave.bpm.BpmLeaveThreadLocalRe;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.engine.impl.el.FixedValue;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service("bpmLeaveEventListener")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class BpmLeaveEventListener implements TaskListener {

    private FixedValue taskNameConfig;

    @Resource
    private BpmLeaveTaskService bpmLeaveTaskService;
    @Resource
    private BpmLeaveDao bpmLeaveDao;

    public void setTaskNameConfig(FixedValue taskNameConfig) {
        this.taskNameConfig = taskNameConfig;
    }
    public FixedValue getTaskNameConfig() {
        return taskNameConfig;
    }

    /**
     * 直接注入会失败，需要使用工厂处理下
     */
    public BpmLeaveEventListener(){
        this.bpmLeaveDao = (BpmLeaveDao) BpmSpringUtils.getBean("bpmLeaveDao");
        this.bpmLeaveTaskService = (BpmLeaveTaskService) BpmSpringUtils.getBean("bpmLeaveTaskService");
    }

    @Override
    public void notify(DelegateTask task) {
        try {
            Map<String, Object> map = task.getVariables();
            // 监听事件类型--任务创建
            if (BpmCommonConstant.BPM_PHASE_CREATE.equals(task.getEventName())) {
                // 创建任务扩展表
                ActFlwServiceDataLeaveVO taskExt = bpmLeaveDao.initTaskExt(task, map);
                // 同时创建实时任务表
                bpmLeaveDao.initTaskExtRu(taskExt);
                // 将人工任务信息放到本地线程变量中，返回给业务系统
                BpmLeaveThreadLocalRe.addThreadLocalData(taskExt);
            }
            // 监听事件类型--任务接收
            if (BpmCommonConstant.BPM_PHASE_ACCEPT.equals(task.getEventName())) {
                // 获取任务记录并更新  检查下这个getAssignee
                bpmLeaveDao.updateTaskExtByAccept(task.getId(), task.getAssignee());
                bpmLeaveDao.updateTaskExtRuByAccept(task.getId(), task.getAssignee());
            }
            // 监听事件类型--任务完成
            if (BpmCommonConstant.BPM_PHASE_COMPLETE.equals(task.getEventName()) ||
                    BpmCommonConstant.BPM_PHASE_DELETE.equals(task.getEventName())) {
                // todo: 暂时取不到当前用户，先使用接口传参模拟
                // 更新任务扩展表
                ActFlwServiceDataLeaveVO taskExt = bpmLeaveDao.updateTaskExtByComplete(task);
                // 删除任务扩展实时数据
                bpmLeaveDao.deleteTaskExtRuVO(taskExt);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("创建、修改任务扩展信息出错：" + e.getMessage());
        }
    }

}
